clear 

*************************

use ${build}/iv_fin_lehd_w_comp_weights.dta, clear
local clusteroption="judgexyear"
local instrument="loo_vc_ivsamp100"
local fe="districtxyearxquarter"

tab district, gen(dist_dum)
gen nbrhd_pov_rate_5yr_sq = nbrhd_pov_rate_5yr^2

replace ad_damnum = ad_damnum 
gen ad_damnum_sq = ad_damnum^2

gen uno = 1

macro define controls="noprior noprior_miss joint ad_damnum ageatcase ageatcase_sq ageatcase_cub fem black white fem_black fem_white hispanic fem_hisp  fem_miss black_miss hispanic_miss nbrhd_pov_rate_5yr nbrhd_pov_rate_5yr_sq nbrhd_pov_rate_5yr_miss  " 
macro define controls_test="0=joint=ad_damnum=ageatcase= ageatcase_sq= ageatcase_cub= fem=black=white=hispanic=noprior=nbrhd_pov_rate_5yr=nbrhd_pov_rate_5yr_sq = fem_black =  fem_white  = fem_hisp" 

macro define  controls_keep "joint ad_damnum noprior ageatcase ageatcase_sq ageatcase_cub fem black white hispanic    nbrhd_pov_rate_5yr  nbrhd_pov_rate_5yr nbrhd_pov_rate_5yr_sq fem_black fem_white fem_hisp"
macro define bin_controls = "joint fem black white  hispanic noprior fem_white fem_black fem_hisp "
macro define cts_controls = "ad_damnum ageatcase nbrhd_pov_rate_5yr  ad_damnum nbrhd_pov_rate_5yr_sq  ageatcase_sq ageatcase_cub"

***********************
*Test of Randomization*
***********************
gen obscount = 1
gen outcome = ""
gen group = ""

eststo rand1: areg c_evict $controls , r cluster(`clusteroption') a(`fe')
	local obs1: di %12.2gc e(N)
	estadd local obs "`obs1'", replace
	test $controls_test
	estadd scalar F=r(F), replace
	estadd scalar p=r(p), replace
	test
	estadd scalar Fall=r(F), replace
	estadd scalar pall=r(p), replace

eststo rand2: areg `instrument' $controls , r cluster(`clusteroption')  a(`fe')
	local obs1: di %12.2gc e(N)
	estadd local obs "`obs1'", replace
	test $controls_test
	estadd scalar F=r(F), replace
	estadd scalar p=r(p), replace
	test
	estadd scalar Fall=r(F), replace
	estadd scalar pall=r(p), replace	


*****************
*Export to Latex*
*****************
esttab  rand1 rand2  using "${Inputs}/Cook_lehd_balance.csv", ///
replace compress nodepvars wrap csv nonotes label  se(5) b(5) ///
eqlabels(none) keep( $controls_keep ) star(* 0.10 ** 0.05 *** 0.01) ///
stats(obs F p Fall pall, fmt(3 3 3 3 3 3)  labels( `"Number of observations"' `"Joint F-Test Stat."' `"p-value"' `"F incld miss ind"' `"p-val incld miss ind"') ) ///
mtitles("Evicted" "Judge Stringency") 
